﻿//-----------------------------------------------------------------------
// <copyright file="MetaKeywords.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.Web
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Xml.Serialization;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Meta keywords class
    /// </summary>
    [Serializable]
    public class MetaKeywords : List<MetaKeyword>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the MetaKeywords class
        /// </summary>
        public MetaKeywords()
        {
        }
        #endregion

        #region Add Method
        /// <summary>
        /// Add keyword to collection
        /// </summary>
        /// <param name="keyword">Keyword value</param>
        public new void Add(MetaKeyword keyword)
        {
            if (this.Count > 25)
            {
                throw new InvalidLengthException("Title element can't be greater than 64 characters, defined by the W3C standard.");
            }

            base.Add(keyword);
        }
        #endregion 

        #region To String Method
        /// <summary>
        /// Convert meta keywords to string for html
        /// </summary>
        /// <returns>String meta keywords</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.Count; i++)
            {
                if (i != this.Count - 1)
                {
                    sb.Append(this[i].Value + ",");
                }
                else
                {
                    sb.Append(this[i].Value);
                }
            }

            return sb.ToString();
        }
        #endregion

        #region Internal Set Keywords Method
        /// <summary>
        /// Set the collection keywords
        /// </summary>
        /// <param name="keywordsXml">Collection as Xml string</param>
        /// <returns>Collection of meta keywords</returns>
        internal static MetaKeywords SetKeywords(string keywordsXml)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(MetaKeywords));
            StringReader stringReader = new StringReader(keywordsXml);
            return (MetaKeywords)xmlSerializer.Deserialize(stringReader);
        }
        #endregion

        #region Internal Get Keywords Method
        /// <summary>
        /// Get the keywords from the collection as string
        /// </summary>
        /// <returns>String of keywords</returns>
        internal string GetKeywords()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(MetaKeywords));
            StringWriter stringWriter = new StringWriter();
            xmlSerializer.Serialize(stringWriter, this);
            return stringWriter.ToString();
        }
        #endregion            
    }
}